package com.shop.resource.service.impl;

import com.alibaba.fastjson.JSON;
import com.shop.resource.service.ISmsService;
import com.teemor.common.bean.utils.tools.HttpTool;
import com.teemor.common.bean.utils.tmpl.SmsBean;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 *
 * @author zhoulk
 * @date: 2019年05月08日  13:55
 */
@Service
public class SmsService implements ISmsService {
    @Value("${sms.host}")
    private String host;
    @Value("${sms.path}")
    private String path;
    @Value("${sms.appcode}")
    private String appcode;
    @Value("${sms.method}")
    private String method;

    @Override
    public Map<String, Object> sendSmsCode(String code, String mobile) {
        String content = "【闲卖网】验证码为：" + code + "，5分钟内有效！";
        return sendSms(content,mobile);
    }

    @Override
    public Map<String, Object> sendSmsMsg(String content, String mobile) {
        return sendSms(content,mobile);
    }

    private Map<String, Object> sendSms(String content, String mobile){
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + appcode);

        Map<String, String> querys = new HashMap<>();
        querys.put("content", content);
        System.out.println(querys.get("content"));
        querys.put("mobile", mobile);

        Map<String, String> bodys = new HashMap<>();
        SmsBean s ;
        Map<String, Object> result = new HashMap<>();
        try {
            HttpResponse response = HttpTool.doPost(host, path, method, headers, querys, bodys);
            s = JSON.parseObject(EntityUtils.toString(response.getEntity()), SmsBean.class);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            return result;
        }
        if (s.getReturnStatus().equals("Success")) {
            result.put("success", true);
        } else {
            result.put("success", false);
        }
        return result;
    }
}
